<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card shadow="hover">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            v-model="queryParams.query"
            class="input-with-select" clearable
            @clear="getGoodsListData()"
            @change="getGoodsListData()"
            >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsListData()"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4"><el-button type="primary" @click="goAddPage">添加商品</el-button></el-col>
      </el-row>

      <el-table
        class="goodsList_table"
        :data="goodsListData"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="500" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(￥)" width="120">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="120">
        </el-table-column>
        <el-table-column prop="create_time_cn" label="创建时间"> </el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
           <el-button type="primary" icon="el-icon-edit" size="mini" round @click="editGood(scope.row)">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" round @click="handleGoodDelete(scope.row)">删除</el-button>

          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pagenum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="queryParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

    </el-card>

    <!-- 删除商品提示框 -->
    <el-dialog title="删除商品" :visible.sync="deleteDialogFormVisible">
      <el-form :model="deleteGoodForm" label-width="120px">
        <el-form-item label="商品名称：" >
          <el-input v-model="deleteGoodForm.goods_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品价格(￥)：">
          <el-input v-model="deleteGoodForm.goods_price" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品重量：">
          <el-input v-model="deleteGoodForm.goods_weight" disabled></el-input>
        </el-form-item>
        <el-form-item label="创建时间：">
          <el-input v-model="deleteGoodForm.create_time_cn" disabled></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteGood()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑商品提示框 -->
    <el-dialog title="编辑商品" :visible.sync="editDialogFormVisible">

      <el-form :model="editGoodForm" :rules="editGoodFormRules" ref="editFormRef" label-width="120px">
        <el-form-item label="商品名称："  prop="goods_name">
          <el-input v-model="editGoodForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格(￥)：" prop="goods_price">
          <el-input v-model="editGoodForm.goods_price" ></el-input>
        </el-form-item>
        <el-form-item label="商品数量：" prop="goods_number">
          <el-input v-model="editGoodForm.goods_number" ></el-input>
        </el-form-item>
        <el-form-item label="商品重量：" prop="goods_weight">
          <el-input v-model="editGoodForm.goods_weight" ></el-input>
        </el-form-item>
        <el-form-item label="创建时间：">
          <el-input v-model="editGoodForm.create_time_cn" disabled></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodConfirm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.getGoodsListData();
  },
  data() {
    return {
      goodsListData: [],
      queryParams: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
      deleteDialogFormVisible:false,
      deleteGoodForm:{},


      editDialogFormVisible:false,
      editGoodForm:{
        
      },
      editGoodFormRules:{
        goods_name: [
            {required: true, message: "请输入商品名称", trigger: "blur"}
        ],
        goods_price: [
            {required: true, message: "请输入商品价格", trigger: "blur"}
        ],
        goods_number: [
            {required: true, message: "请输入商品数量", trigger: "blur"}
        ],
        goods_weight: [
            {required: true, message: "请输入商品重量", trigger: "blur"}
        ],
      },

    };
  },
  methods: {
    async getGoodsListData() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryParams,
      });
      if (res.meta.status !== 200)
        return this.$message.error("商品列表数据获取失败");
      // console.log(res.data);
      this.goodsListData = res.data.goods.map(v=>({...v, create_time_cn:(new Date(v.add_time * 1000).toLocaleString())}));

      this.total = res.data.total;
    },
    handleSizeChange(size){
      this.queryParams.pagesize = size;
      this.getGoodsListData();
    },
    handleCurrentChange(page){
      this.queryParams.pagenum = page;
      this.getGoodsListData();
    },
    handleGoodDelete(row){
      
      this.deleteDialogFormVisible = true;
      this.deleteGoodForm = row;
      this.deleteGoodForm.create_time_cn = new Date(row.add_time * 1000).toLocaleString();
      // console.log(this.deleteGoodForm);
    },
    async deleteGood(){
      const {data:res} = await this.$http.delete(`goods/${this.deleteGoodForm.goods_id}`);
      if(res.meta.status !== 200) return this.$message.error("商品删除失败");
      this.getGoodsListData();
      this.deleteDialogFormVisible = false;
      this.$message.success("商品删除成功");
    },
    goAddPage(){
      this.$router.push('/goods/add');
    },
    editGood(row){
      console.log(row);
      this.editDialogFormVisible = true;
      this.editGoodForm = row;
      this.editGoodForm.create_time_cn = new Date(row.add_time * 1000).toLocaleString();
    },
    editGoodConfirm(){
      this.$refs.editFormRef.validate(async (valid) => {
        if(!valid) return this.$message.error('商品修改信息校验失败');
        const {data:res} = await this.$http.put(`goods/${this.editGoodForm.goods_id}`, {goods_name:this.editGoodForm.goods_name, goods_price:this.editGoodForm.goods_price, goods_number:this.editGoodForm.goods_number, goods_weight:this.editGoodForm.goods_weight});
        // 此处修改不成功，显示token无效，需要加入token
        if(res.meta.status !== 201) return this.$message.error(res.meta.msg);
        this.getGoodsListData();
        this.editDialogFormVisible = false;
        this.$message.success('商品信息修改成功');
      
      });
    }
    
  },
  computed: {},
};
</script>

<style lang="less" scoped>
.goodsList_table {
  margin-top: 20px;
}

.el-pagination{
  margin-top: 20px;
}
</style>